package ltd.hxya.novel.admin.controller;

import ltd.hxya.novel.admin.service.CacheService;
import ltd.hxya.novel.admin.vo.Cache;
import ltd.hxya.novel.common.bean.ModuleEnum;
import ltd.hxya.novel.common.bean.OperEnum;
import ltd.hxya.novel.common.bean.Result;
import ltd.hxya.novel.common.constant.OperatorConstant;
import ltd.hxya.novel.common.anno.OperLogAnno;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin/cache")
public class CacheController {

    @Autowired
    private CacheService cacheService;

    //查询缓存列表
    @GetMapping("/cacheNames")
    public Result<List<Cache>> cacheNames(){
        List<Cache> cacheName = cacheService.cacheNames();
        return Result.success(cacheName);
    }

    @GetMapping("/cacheData")
    public Result<String> cacheData(String key){
        String data = cacheService.cacheData(key);
        return Result.success(data);
    }

    @OperLogAnno(
            module = ModuleEnum.CACHE_MONITOR,
            operator = OperEnum.DELETE,
            desc = "根据键删除缓存中对应的内容")
    @DeleteMapping("/deleteKey")
    //localhost:8003/api/admin/cache/deleteKey?key=3
    public Result deleteKey(String key){
        cacheService.deleteKey(key);
        return Result.success();
    }

    @OperLogAnno(module = ModuleEnum.CACHE_MONITOR,operator = OperEnum.DELETE,desc = "清空缓存中的所有键值对")
    @DeleteMapping("/clear")
    public Result clear(){
        cacheService.clearCache();
        return Result.success();
    }
}
